<?php

// +----------------------------------------------------------------------
// | 前台会员中心Action Base
// +----------------------------------------------------------------------

namespace User\Controller;

use Common\Controller\Base;

class UserbaseController extends Base {

    //会员模型相关配置
    protected $userConfig = array();
    //用户id
    protected $userid = 0;
    //用户名
    protected $uname = NULL;
    //用户信息
    protected $userinfo = array();
    //是否connect登陆
    protected $isConnectLogin = false;

    protected function _initialize() {
        $this->check_user();

        parent::_initialize();

        //会员参数设置
        $this->userConfig = array(
            'qq_akey'=>'', //QQ登录密钥
            'qq_skey'=>'', //QQ登录密钥
            'defaultupoint' => 50, //默认积分
            'enablemailcheck'=>true //发送邮件
        );

        $this->userDb = D('User/User');

    }

    /**
     * 检测用户是否已经登陆 
     */
    final public function check_user() {
        $this->userid = (int) service("Passport")->userid;
        $this->username = service("Passport")->uname;
   //     $this->userinfo = service("Passport")->getInfo();
        $this->assign('userid', $this->userid);
        $this->assign('uname', $this->uname);

        //检查是否授权登陆
        $connect_app = session('connect_app');
        if (!empty($connect_app)) {
            $this->isConnectLogin = true;
        }
        if (substr(ACTION_NAME, 0, 7) == 'public_') {
            //所有以public_开头的方法都无需检测是否登陆
            return true;
        }
        //特定模块无需登陆验证
        if (MODULE_NAME == 'User' && in_array(CONTROLLER_NAME, array('Login'))) {
            //Public,Home不用验证登陆
            if (empty($this->userid)) {
                $this->assign('userid', 0);
            }
            return true;
        }
        //特定方法无需登陆验证
        if (MODULE_NAME == 'User' && CONTROLLER_NAME == 'Index' && in_array(ACTION_NAME, array('register', 'logout', 'lostpassword', 'resetpassword', 'verifyemail'))) {
            //该类方法不需要验证是否登陆
            return true;
        }
        if ($this->userid) {
            //锁定用户
            if ($this->userinfo['islock'] == 1) {
                service("Passport")->logoutLocal();
                $this->error("您的帐号已经被锁定！", self::$Cache['Config']['siteurl']);
            }
            return true;
        } else {
            service("Passport")->logoutLocal(); //注销
            //如果未登录,记录上一个页面,登录后返回
            cookie('backurl',get_url(),3600);
            $this->redirect('/user/login/index?t=rd');
        }
    }


}
